    package com.example.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {

    private int defalutSize;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);

        defalutSize = a.getDimensionPixelSize(R.styleable.MyView_default_size,100);

        a.recycle();

    }

    private int getMySize(int defalutSize,int measureSpec){
        int mySize = defalutSize;

        int mode = MeasureSpec.getMode(measureSpec);
        int size  = MeasureSpec.getSize(measureSpec);

        switch (mode){
            case MeasureSpec.UNSPECIFIED:{
                mySize = defalutSize;
                break;
            }
            case MeasureSpec.AT_MOST:{
                mySize = size;
                break;
            }
            case MeasureSpec.EXACTLY:{
                mySize = size;
                break;
            }
        }
        return mySize;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMySize(defalutSize,widthMeasureSpec);
        int height= getMySize(defalutSize,heightMeasureSpec);

        if (width < height){

            height = width;

        }else {
            width = height;
        }

        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int r = getMeasuredWidth() / 2;

        int centerX = getLeft() + r;

        int centerY = getTop() + r;

        Paint paint = new Paint();
        paint.setColor(Color.GREEN);

        canvas.drawCircle(centerX,centerY,r,paint);

    }


}
